home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / DEMON / LANGUAGE / POT.ARC / pot / Docu / OberonRepo next >
Text File  |  1995-01-22  |  47KB  |  1,020 lines

  1.     
  2.                      The Programming Language Oberon
  3.                        (Revision 1. 10. 90) N.Wirth
  4.                         Make it as simple as possible, but not simpler.
  5.                                                             A. Einstein
  6.     
  7.     1. Introduction
  8.         Oberon is a general-purpose programming language  that  evolved
  9.     from  Modula-2.  Its  principal  new feature is the concept of type
  10.     extension.It permits the construction of  new  data  types  on  the
  11.     basis of existing ones and to relate them.
  12.         This report is not intended as a programmer's tutorial.  It  is
  13.     intentionally kept concise. Its function is to serve as a reference
  14.     for programmers,  implementors,  and manual writers.  What  remains
  15.     unsaid  is  mostly  left  so  intentionally,  either  because it is
  16.     derivable from stated rules of the language,  or because  it  would
  17.     require  to commit the definition when a general commitment appears
  18.     as unwise.
  19.     
  20.     2. Syntax
  21.        A language   is  an  infinite  set  of  sentences,  namely  the
  22.     sentences well formed according to its syntax.  In  Oberon,  these
  23.     sentences  are  called  compilation  units.  Each unit is a finite
  24.     sequence of symbols from a finite vocabulary.  The  vocabulary  of
  25.     Oberon  consists  of  identifiers,  numbers,  strings,  operators,
  26.     delimiters,  and comments. They are called lexical symbols and are
  27.     composed of sequences of characters. (Note the distinction between
  28.     symbols and characters.)  To  describe  the  syntax,  an  extended
  29.     Backus-Naur Formalism called EBNF is used. Brackets [ and ] denote
  30.     optionality of the enclosed sentential form,  and braces {  and  }
  31.     denote  its  repetition  (possibly  0  times).  Syntactic entities
  32.     (non-terminal symbols) are denoted  by  English  words  expressing
  33.     their  intuitive  meaning.  Symbols  of  the  language  vocabulary
  34.     (terminal symbols) are denoted by strings enclosed in quote  marks
  35.     or  words  written  in capital letters,  so-called reserved words.
  36.     Syntactic rules (productions) are marked by a $ sign at  the  left
  37.     margin of the line.
  38.     
  39.     3. Vocabulary and representation
  40.        The representation of symbols in terms of characters is defined
  41.     using the ASCII set.  Symbols are identifiers,  numbers,  strings,
  42.     operators,  delimiters,  and comments. The following lexical rules
  43.     must  be  observed.  Blanks  and line breaks must not occur within
  44.     symbols (except in comments,  and blanks  in  strings).  They  are
  45.     ignored  unless  they  are  essential  to separate two consecutive
  46.     symbols.  Capital and lower-case letters are considered  as  being
  47.     distinct.
  48.     
  49.                                     1.
  50.     
  51.        Identifiers are  sequences  of  letters  and digits.  The first
  52.     character must be a letter.
  53.     
  54.     $ ident  =  letter {letter | digit}.
  55.     
  56.     Examples: x scan   Oberon   GetSymbol   firstLetter
  57.     
  58.                                     2.
  59.     
  60.        Numbers are (unsigned) integers or real numbers.  Integers  are
  61.     sequences  of  digits and may be followed by a suffix letter.  The
  62.     type is the minimal type to which the number belongs  (see  6.1.).
  63.     If  no  suffix  is specified,  the representation is decimal.  The
  64.     suffix H  indicates  hexadecimal  representation.  A  real  number
  65.     always contains a decimal point.  Optionally it may also contain a
  66.     decimal scale factor.  The letter E (or D) is pronounced as "times
  67.     ten to the power of". A real number is of type REAL, unless it has
  68.     a scale factor containing the letter D; in this case it is of type
  69.     LONGREAL.
  70.     
  71.     $ number = integer | real.
  72.     $ integer = digit {digit} | digit {hexDigit}  "H"  .
  73.     $  real  =  digit  {digit}  "."  {digit}[ScaleFactor].
  74.     $  ScaleFactor  =  ("E"  |  "D") ["+" | "-"] digit{digit}.
  75.     $ hexDigit =  digit | "A" | "B" | "C" | "D" | "E" | "F".
  76.     $ digit  =  "0" | "1" | "2" | "3" | "4" | "5" | "6" |
  77.                 "7" | "8" | "9".
  78.     
  79.     Examples:
  80.        1987
  81.        100H = 256
  82.        12.3
  83.        4.567E8 = 456700000
  84.        0.57712566D-6 = 0.00000057712566
  85.     
  86.                                     3.
  87.        Character constants are either denoted by  a  single  character
  88.     enclosed  in quote marks or by the ordinal number of the character
  89.     in hexadecimal notation followed by the letter X.
  90.     
  91.     $ CharConstant = """ character """ | digit {hexDigit} "X".
  92.     
  93.                                     4.
  94.     
  95.        Strings are sequences of characters  enclosed  in  quote  marks
  96.     (").  A  string  cannot  contain  a  quote  mark.  The  number  of
  97.     characters in a string is called the length of the string. Strings
  98.     can be assigned to and compared with arrays of characters (see 9.1
  99.     and 8.2.4).
  100.     
  101.     $ string  =  """ {character}""" .
  102.     
  103.     Examples: "OBERON"    "Don't worry!"
  104.     
  105.                                     5.
  106.     
  107.        Operators and delimiters are the special characters,  character
  108.     pairs,  or  reserved  words  listed  below.  These  reserved words
  109.     consist exclusively of capital letters and cannot be used  in  the
  110.     role of identifiers.
  111.     
  112.     +   :=    ARRAY   IS            TO
  113.     -   ^     BEGIN   LOOP          TYPE
  114.     *   =     CASE    MOD           UNTIL
  115.     /   #     CONST   MODULE        VAR
  116.     ~   <     DIV     NIL           WHILE
  117.     &   >     DO      OF            WITH
  118.     .   <=    ELSE    OR
  119.     ,   >=    ELSIF   POINTER
  120.     ;   ..    END     PROCEDURE
  121.     |   :     EXIT    RECORD
  122.     (   )     IF      REPEAT
  123.     [   ]     IMPORT  RETURN
  124.     {   }     IN      THEN
  125.     
  126.                                     6.
  127.     
  128.        Comments may be inserted between any two symbols in a  program.
  129.     They  are  arbitrary  character sequences opened by the bracket (*
  130.     and closed by *). Comments do not affect the meaning of a program.
  131.     
  132.     4. Declarations and scope rules
  133.        Every identifier occurring in a program must be introduced by a
  134.     declaration,  unless it is a predefined  identifier.  Declarations
  135.     also  serve  to specify certain permanent properties of an object,
  136.     such as whether it is  a  constant,  a  type,  a  variable,  or  a
  137.     procedure.  The identifier is then used to refer to the associated
  138.     object.  This is possible in those parts of a program  only  which
  139.     are within the scope of the declaration.  No identifier may denote
  140.     more than one object within  a  given  scope.  The  scope  extends
  141.     textually  from  the  point  of  the declaration to the end of the
  142.     block (procedure or module) to which the declaration  belongs  and
  143.     hence to which the object is local.
  144.        The scope rule has the following amendments:
  145.        1. If  a  type  T  is  defined as POINTER TO T1 (see 6.4),  the
  146.     identifier T1 can be declared textually following the  declaration
  147.     of T, but it must lie within the same scope.
  148.        2. Field identifiers of a  record  declaration  (see  6.3)  are
  149.     valid in field designators only. In its declaration, an identifier
  150.     in the global scope may be followed  by  an  export  mark  (*)  to
  151.     indicate  that  it be exported from its declaring module.  In this
  152.     case,  the identifier may be used in other modules, if they import
  153.     the  declaring  module.  The  identifier  is  then prefixed by the
  154.     identifier designating its module (see Ch. 11). The prefix and the
  155.     identifier  are  separated  by  a period and together are called a
  156.     qualified identifier.
  157.     
  158.     $ qualident = [ident "."] ident.
  159.     $ identdef = ident ["*"].
  160.     
  161.     The following identifiers are predefined; their meaning is defined
  162.     in the indicated sections:
  163.     
  164.     ABS       (10.2)    LEN       (10.2)   ASH       (10.2)
  165.     LONG      (10.2)    BOOLEAN   (6.1)    LONGINT   (6.1)
  166.     BYTE      (6.1)     LONGREAL  (6.1)    CAP       (10.2)
  167.     MAX       (10.2)    CHAR      (6.1)    MIN       (10.2)
  168.     CHR       (10.2)    NEW       (6.4)    DEC       (10.2)
  169.     ODD       (10.2)    ENTIER    (10.2)   ORD       (10.2)
  170.     EXCL      (10.2)    REAL      (6.1)    FALSE     (6.1)
  171.     SET       (6.1)     HALT      (10.2)   SHORT     (10.2)
  172.     INC       (10.2)    SHORTINT  (6.1)    INCL      (10.2)
  173.     SIZE      (10.2)    INTEGER  (6.1)     TRUE      (6.1)
  174.     
  175.     5. Constant declarations
  176.        A constant declaration associates an identifier with a constant
  177.     value.
  178.     
  179.     $ ConstantDeclaration  =  identdef "=" ConstExpression.
  180.     $ ConstExpression  =  expression.
  181.     
  182.        A constant  expression  can be evaluated by a mere textual scan
  183.     without actually executing the program. Its operands are constants
  184.     (see Ch. 8). Examples of constant declarations are
  185.     
  186.        N = 100
  187.        limit = 2*N -1
  188.        all = {0 .. WordSize-1}
  189.     
  190.     6. Type declarations
  191.        A data type determines the set of  values  which  variables  of
  192.     that  type  may assume,  and the operators that are applicable.  A
  193.     type declaration is used to associate an identifier with the type.
  194.        Such association may be with unstructured (basic) types,  or it
  195.     may be with  structured  types,  in  which  case  it  defines  the
  196.     structure  of  variables  of  this type and,  by implication,  the
  197.     operators that are applicable to the  components.  There  are  two
  198.     different  structures,  namely arrays and records,  with different
  199.     component selectors.
  200.     
  201.     $ TypeDeclaration  =  identdef "=" type.
  202.     $ type  =  qualident| ArrayType | RecordType |
  203.                         PointerType | ProcedureType.
  204.     
  205.     Examples:
  206.     Table = ARRAY N OF REAL
  207.     Tree = POINTER TO Node
  208.     
  209.     Node = RECORD
  210.       key:INTEGER;
  211.       left, right: Tree
  212.     END
  213.     
  214.     CenterNode = RECORD (Node)
  215.        name: ARRAY 32 OF CHAR;
  216.        subnode: Tree
  217.     END
  218.     
  219.     Function* = PROCEDURE(x: INTEGER): INTEGER
  220.     
  221.     6.1. Basic types
  222.        The following   basic   types   are   denoted   by  predeclared
  223.     identifiers.  The associated operators are defined in 8.2, and the
  224.     predeclared  function  procedures  in 10.2.  The values of a given
  225.     basic type are the following:
  226.     1. BOOLEAN the truth values TRUE and FALSE.
  227.     2. CHAR the characters of the extended ASCII set  (0X ... 0FFX).
  228.     3. SHORTINT the integers between -128 and 127.
  229.     4. INTEGER the integers between MIN(INTEGER) and MAX(INTEGER).
  230.     5. LONGINT the integers between MIN(LONGINT) and MAX(LONGINT).
  231.     6. REAL real numbers between MIN(REAL) and MAX(REAL).
  232.     7. LONGREAL real numbers between MIN(LONGREAL) and MAX(LONGREAL).
  233.     8. SET the sets of integers between 0 and MAX(SET).
  234.        Types 3 to 5 are integer types,  6 and 7 are  real  types,  and
  235.     together they are called numeric types. They form a hierarchy; the
  236.     larger type includes (the values of) the smaller type:
  237.        LONGREAL >= REAL >= LONGINT >= INTEGER >= SHORTINT
  238.     
  239.     6.2. Array types
  240.        An array  is  a  structure  consisting  of  a  fixed  number of
  241.     elements which are all of the same type, called the element type.
  242.        The number of elements of an array is called its length.
  243.        The elements of the array are designated by indices,  which are
  244.     integers between 0 and the length minus 1.
  245.     
  246.     $ ArrayType = ARRAY length {"," length} OF type.
  247.     $ length  =  ConstExpression.
  248.        A declaration  of  the  form
  249.                     ARRAY N0,  N1,  ...  ,  Nk OF T
  250.     is understood as an abbreviation of the declaration
  251.                 ARRAY N0 OF ARRAY N1 OF ... ARRAY Nk OF T
  252.     Examples of array types:
  253.     ARRAY N OF INTEGER 
  254.     ARRAY 10, 20 OF REAL
  255.     
  256.     6.3. Record types
  257.        A record  type  is  a structure consisting of a fixed number of
  258.     elements of possibly different types.  The record type declaration
  259.     specifies  for  each  element,  called  field,  its  type  and  an
  260.     identifier which denotes the  field.  The  scope  of  these  field
  261.     identifiers  is  the  record definition itself,  but they are also
  262.     visible within field designators (see 8.1) referring  to  elements
  263.     of record variables.
  264.     $ RecordType  =  RECORD ["("BaseType ")"] FieldListSequence END.
  265.     $ BaseType  =  qualident.
  266.     $ FieldListSequence  =  FieldList {";" FieldList}.
  267.     $ FieldList =  [IdentList ":" type].
  268.     $ IdentList  =  identdef {"," identdef}.
  269.        If a record type is exported,  field identifiers that are to be
  270.     visible  outside  the  declaring  module must be marked.  They are
  271.     called public fields ;  unmarked fields are called private fields.
  272.     Record types are extensible,  i.e. a record type can be defined as
  273.     an extension of  another  record  type.  In  the  examples  above,
  274.     CenterNode  (directly)  extends  Node,  which is the (direct) base
  275.     type of CenterNode.  More specifically,  CenterNode  extends  Node
  276.     with the fields name and subnode.  Definition: A type T0 extends a
  277.     type T,  if it equals T, or if it directly extends an extension of
  278.     T.  Conversely, a type T is a base type of T0, if it equals T0, or
  279.     if it is the direct base type of a base type of  T0.
  280.        Examples of record types:
  281.     RECORD
  282.       day,  month,  year: INTEGER
  283.     END
  284.     RECORD
  285.       name, firstname: ARRAY 32 OF CHAR;
  286.       age: INTEGER;
  287.       salary: REAL
  288.     END
  289.     
  290.     6.4. Pointer types
  291.        Variables of a pointer type P  assume  as  values  pointers  to
  292.     variables  of some type T.  The pointer type P is said to be bound
  293.     to T,  and T is the pointer base type of P.  T must be a record or
  294.     array type.  Pointer types inherit the extension relation of their
  295.     base types.  If a type T0 is an extension of T and P0 is a pointer
  296.     type bound to T0, then P0 is also an extension of P.
  297.     $ PointerType = POINTER TO type.
  298.        If p is a variable of type P = POINTER TO T, then a call of the
  299.     predefined procedure NEW(p) has the following effect (see 10.2):
  300.        A variable  of  type  T  is  allocated  in free storage,  and a
  301.     pointer to it is assigned to p.  This pointer p is of type P;  the
  302.     referenced variable p^ is of type T. Failure of allocation results
  303.     in p obtaining the value NIL.
  304.        Any pointer  variable  may  be  assigned  the value NIL,  which
  305.     points to no variable at all.
  306.     
  307.     6.5. Procedure types
  308.         Variables of  a  procedure  type T have a procedure (or NIL) as
  309.     value. If a procedure P is assigned to a procedure variable of type
  310.     T,  the  (types  of the) formal parameters of P must be the same as
  311.     those indicated in the formal parameters of T.  The same holds  for
  312.     the  result type in the case of a function procedure (see 10.1).  P
  313.     must not be declared local to another procedure, and neither can it
  314.     be a predefined procedure.
  315.     
  316.     $ ProcedureType = PROCEDURE [FormalParameters].
  317.     
  318.     7. Variable declarations
  319.         Variable declarations  serve   to   introduce   variables   and
  320.     associate  them  with  identifiers  that  must be unique within the
  321.     given scope. They also serve to associate fixed data types with the
  322.     variables.
  323.     
  324.     $ VariableDeclaration  =  IdentList ":" type.
  325.     
  326.         Variables whose  identifiers appear in the same list are all of
  327.     the same type.
  328.     Examples of variable declarations (refer to examples in Ch. 6):
  329.     i, j, k: INTEGER
  330.     x, y: REAL
  331.     p, q: BOOLEAN
  332.     s:   SET
  333.     f:   Function
  334.     a:   ARRAY 100 OF REAL
  335.     w:   ARRAY 16 OF
  336.       RECORD
  337.         ch: CHAR;
  338.         count: INTEGER
  339.       END
  340.     t: Tree
  341.     
  342.     8. Expressions
  343.         Expressions are  constructs  denoting  rules   of   computation
  344.     whereby  constants  and current values of variables are combined to
  345.     derive other values by the application of  operators  and  function
  346.     procedures.   Expressions   consist   of  operands  and  operators.
  347.     Parentheses  may  be  used  to  express  specific  associations  of
  348.     operators and operands.
  349.     
  350.     8.1. Operands
  351.         With the exception of sets and literal constants,  i.e. numbers
  352.     and  character  strings,  operands  are  denoted by designators.  A
  353.     designator consists of an identifier  referring  to  the  constant,
  354.     variable,  or  procedure  to  be  designated.  This  identifier may
  355.     possibly be qualified by module identifiers (see Ch. 4 and 11), and
  356.     it  may  be  followed by selectors,  if the designated object is an
  357.     element of a structure. If A designates an array, then A[E] denotes
  358.     that  element  of  A  whose  index  is  the  current  value  of the
  359.     expression E.  The type of E must be an integer type.  A designator
  360.     of the form A[E1, E2, ... , En] stands for A[E1][E2] ... [En]. If p
  361.     designates a pointer variable,  p^ denotes the  variable  which  is
  362.     referenced  by  p.  If r designates a record,  then r.f denotes the
  363.     field f of r. If p designates a pointer, p.f denotes the field f of
  364.     the  record p^,  i.e.  the dot implies dereferencing and p.f stands
  365.     for p^.f,  and p[E] denotes the element of p^  with  index  E.  The
  366.     typeguard  v(T0)  asserts  that  v  is of type T0,  i.e.  it aborts
  367.     program  execution,  if  it  is  not  of  type  T0.  The  guard  is
  368.     applicable, if
  369.     1. T0 is an extension of the declared type T of v, and if
  370.     2. v is a variable parameter of record type or v is a pointer.
  371.     
  372.     $ designator  =  qualident{"." ident | "[" ExpList "]" |
  373.     $                  "(" qualident ")" | "^" }.
  374.     $ ExpList =  expression {"," expression}.
  375.     
  376.         If the  designated  object  is a variable,  then the designator
  377.     refers to  the  variable's  current  value.  If  the  object  is  a
  378.     procedure,  a  designator  without  parameter  list  refers to that
  379.     procedure.  If it is followed by a (possibly empty) parameter list,
  380.     the  designator  implies  an activation of the procedure and stands
  381.     for the value resulting from its execution.
  382.         The (types  of  the)  actual  parameters must correspond to the
  383.     formal parameters as specified in the procedure's declaration  (see
  384.     Ch. 10).
  385.     Examples of designators (see examples in Ch. 7):
  386.     i (INTEGER)
  387.     a[i] (REAL)
  388.     w[3].ch (CHAR)
  389.     t.key (INTEGER)
  390.     t.left.right (Tree)
  391.     t(CenterNode).subnode (Tree)
  392.     
  393.     8.2. Operators
  394.         The syntax of expressions distinguishes between four classes of
  395.     operators  with  different  precedences  (binding  strengths).  The
  396.     operator ~ has the highest precedence,  followed by  multiplication
  397.     operators, addition operators, and relations. Operators of the same
  398.     precedence associate from left to right.  For example, x-y-z stands
  399.     for (x-y)-z.
  400.     
  401.     $ expression  =  SimpleExpression [relation SimpleExpression].
  402.     $ relation  =  "=" | "#" | "<" | "<=" | ">" | ">=" | IN | IS.
  403.     $ SimpleExpression  =  ["+"|"-"] term {AddOperator term}.
  404.     $ AddOperator  =  "+" | "-" | OR .
  405.     $ term  =  factor {MulOperator factor}.
  406.     $ MulOperator  =  "*" | "/" | DIV | MOD | "&" .
  407.     $ factor =  number | CharConstant | string | NIL | set .
  408.     $ designator [ActualParameters] | "(" expression ")" | "~" factor.
  409.     $ set  = "{" [element {"," element}] "}".
  410.     $ element  =  expression [".."expression].
  411.     $ ActualParameters  =  "(" [ExpList] ")" .
  412.     
  413.         The available operators are listed in the following tables.  In
  414.     some instances,  several different operations are designated by the
  415.     same operator symbol.  In these  cases,  the  actual  operation  is
  416.     identified by the type of the operands.
  417.     
  418.     8.2.1. Logical operators
  419.     symbol   result
  420.     OR       logical disjunction
  421.     &        logical conjunction
  422.     ~        negation
  423.         These operators apply to BOOLEAN operands and yield  a  BOOLEAN
  424.     result. p OR q stands for "if p then TRUE, else q" p & q stands for
  425.     "if p then q, else FALSE" ~ p stands for "not p"
  426.     
  427.     8.2.2. Arithmetic operators
  428.     symbol result
  429.     +      sum
  430.     -      difference
  431.     *      product
  432.     /      quotient
  433.     DIV    integer quotient
  434.     MOD    modulus
  435.         The operators +,  -,  *,  and / apply to  operands  of  numeric
  436.     types. The type of the result is that operand's type which includes
  437.     the other operand's type, except for division (/), where the result
  438.     is  the  real type which includes both operand types.  When used as
  439.     operators with a single operand,  - denotes sign  inversion  and  +
  440.     denotes the identity operation.  The operators DIV and MOD apply to
  441.     integer operands only.  They are related by the following  formulas
  442.     defined for any dividend x and positive divisors y:
  443.     x  =  (x DIV y) * y  +  (x MOD y)
  444.     0 <= (x MOD y) < y .
  445.     
  446.     8.2.3.  Set operators
  447.     symbol result
  448.     +      union
  449.     -      difference
  450.     *      intersection
  451.     /      symmetric set difference
  452.         The monadic  minus  sign denotes the complement of x,  i.e.  -x
  453.     denotes the set of integers between 0 and MAX(SET)  which  are  not
  454.     elements of x.
  455.     x - y =  x * (-y)
  456.     x / y =  (x-y) + (y-x)
  457.     
  458.     8.2.4. Relations
  459.     symbol relation
  460.     =   equal
  461.     #   unequal
  462.     <   less
  463.     <=  less or equal
  464.     >   greater
  465.     >=  greater or equal
  466.     IN  set membership
  467.     IS  type test
  468.         Relations are Boolean.  The ordering relations <, <=, >, and >=
  469.     apply to the numeric types,  CHAR,  and character arrays (strings).
  470.     The  relations  =  and # also apply to the type BOOLEAN and to set,
  471.     pointer, and procedure types.
  472.     
  473.     x IN s
  474.         stands for  "x  is  an  element of s".  x must be of an integer
  475.     type, and s of type SET.
  476.     
  477.     v IS T
  478.         stands for  "v  is of type T" and is called a type test.  It is
  479.     applicable, if
  480.         1. T is an extension of the declared type T0 of v, and if
  481.         2. v is a variable parameter of record type or v is a pointer.
  482.         Assuming, for instance, that T is an extension of T0 and that v
  483.     is a designator declared of  type  T0,  then  the  test  "v  IS  T"
  484.     determines  whether the actually designated variable is (not only a
  485.     T0, but also) a T. The value of NIL IS T is undefined.
  486.         Examples of expressions (refer to examples in Ch. 7):
  487.     1987              (INTEGER)
  488.     i DIV 3           (INTEGER)
  489.     ~p OR q           (BOOLEAN)
  490.     (i+j) * (i-j)     (INTEGER)
  491.     s - {8, 9, 13}    (SET)
  492.     i + x             (REAL)
  493.     a[i+j] * a[i-j]   (REAL)
  494.     (0<=i) & (i<100)  (BOOLEAN)
  495.     t.key = 0         (BOOLEAN)
  496.     k IN {i .. j-1}   (BOOLEAN)
  497.     t IS CenterNode   (BOOLEAN)
  498.     
  499.                                9. Statements
  500.     
  501.         Statements denote actions.  There are elementary and structured
  502.     statements.  Elementary  statements  are  not composed of any parts
  503.     that are  themselves  statements.  They  are  the  assignment,  the
  504.     procedure  call,  and  the  return and exit statements.  Structured
  505.     statements are composed of parts that  are  themselves  statements.
  506.     They are used to express sequencing and conditional, selective, and
  507.     repetitive execution.  A statement may also be empty, in which case
  508.     it  denotes no action.  The empty statement is included in order to
  509.     relax punctuation rules in statement sequences.
  510.     
  511.     $ statement  =  [ assignment | ProcedureCall | IfStatement |
  512.     $                 CaseStatement | WhileStatement | RepeatStatement |
  513.     $                 LoopStatement | WithStatement | EXIT |
  514.     $                 RETURN[expression] ].
  515.     
  516.     9.1. Assignments
  517.         The assignment  serves  to  replace  the  current  value  of  a
  518.     variable by a new value specified by an expression.  The assignment
  519.     operator is written as ":=" and pronounced as becomes.
  520.     
  521.     $ assignment  =  designator ":=" expression.
  522.     
  523.         The type of the expression must be included by the type of  the
  524.     variable, or it must extend the type of the variable. The following
  525.     exceptions hold:
  526.         1. The constant NIL can be assigned to variables of any pointer
  527.     or procedure type.
  528.         2. Strings  can  be  assigned  to any variable whose type is an
  529.     array of characters, provided the length of the string is less than
  530.     that  of  the  array.  If  a string s of length n is assigned to an
  531.     array a , the result is a[i] = si for i = 0 ... n-1, and a[n] = 0X.
  532.         Examples of assignments (see examples in Ch. 7):
  533.     i := 0
  534.     p := i = j
  535.     x := i + 1
  536.     k := log2(i+j)
  537.     F := log2
  538.     s := {2, 3, 5, 7, 11, 13}
  539.     a[i] := (x+y) * (x-y)
  540.     t.key := i
  541.     w[i+1].ch := "A"
  542.     
  543.     9.2. Procedure calls
  544.         A procedure call serves to activate a procedure.  The procedure
  545.     call may contain a list of actual parameters which are  substituted
  546.     in  place  of  their corresponding formal parameters defined in the
  547.     procedure  declaration  (see  Ch.  10).   The   correspondence   is
  548.     established  by  the  positions  of  the parameters in the lists of
  549.     actual and formal parameters respectively.
  550.         There exist   two  kinds  of  parameters:  variable  and  value
  551.     parameters.  In  the  case  of  variable  parameters,  the   actual
  552.     parameter   must  be  a  designator  denoting  a  variable.  If  it
  553.     designates an element of a structured  variable,  the  selector  is
  554.     evaluated  when  the  formal/actual  parameter  substitution  takes
  555.     place, i.e. before the execution of the procedure. If the parameter
  556.     is a value parameter, the corresponding actual parameter must be an
  557.     expression.  This expression is evaluated prior  to  the  procedure
  558.     activation,  and  the  resulting  value  is  assigned to the formal
  559.     parameter which now constitutes a local variable (see also  10.1.).
  560.     
  561.     $ ProcedureCall = designator [ActualParameters].
  562.     
  563.     Examples of procedure calls:
  564.     ReadInt(i) (see Ch. 10)
  565.     WriteInt(j*2+1,6)
  566.     INC(w[k].count)
  567.     
  568.     9.3. Statement sequences
  569.     Statement sequences denote the sequence of actions specified by the
  570.     component statements which are separated by semicolons.
  571.     
  572.     $ StatementSequence  =  statement {";" statement}.
  573.     
  574.     9.4. If statements
  575.     
  576.     $ IfStatement  = IF expression THEN StatementSequence
  577.     $ {ELSIF expression THEN StatementSequence}
  578.     $ [ELSE StatementSequence]
  579.     $ END.
  580.     
  581.         If statements specify  the  conditional  execution  of  guarded
  582.     statements.  The Boolean expression preceding a statement is called
  583.     its guard.
  584.         The guards  are evaluated in sequence of occurrence,  until one
  585.     evaluates to TRUE,  whereafter its associated statement sequence is
  586.     executed.   If  no  guard  is  satisfied,  the  statement  sequence
  587.     following the symbol ELSE is executed, if there is one.
  588.         Example:
  589.     IF (ch >= "A") & (ch <= "Z") THEN
  590.       ReadIdentifier
  591.     ELSIF (ch >= "0") & (ch <= "9") THEN
  592.       ReadNumber
  593.     ELSIF ch = 22X THEN ReadString
  594.     END
  595.     
  596.     9.5. Case statements
  597.         Case statements  specify  the  selection  and  execution  of  a
  598.     statement  sequence according to the value of an expression.  First
  599.     the case expression is evaluated,  then the statement  sequence  is
  600.     executed  whose  case  label list contains the obtained value.  The
  601.     case expression and all labels must be of the same type, which must
  602.     be an integer type or CHAR. Case labels are constants, and no value
  603.     must occur more than once.  If the value of the expression does not
  604.     occur as a label of any case,  the statement sequence following the
  605.     symbol  ELSE  is  selected,  if  there  is  one.  Otherwise  it  is
  606.     considered as an error.
  607.  
  608.     $ CaseStatement = CASE expression OF case {"|" case}
  609.     $                [ELSE StatementSequence] END.
  610.     $ case  = [CaseLabelList ":" StatementSequence].
  611.     $ CaseLabelList  = CaseLabels {"," CaseLabels}.
  612.     $ CaseLabels = ConstExpression [".." ConstExpression].
  613.         Example:
  614.     CASE ch OF "A" .. "Z":
  615.       ReadIdentifier
  616.     | "0" .. "9":
  617.       ReadNumber
  618.     | 22X :
  619.       ReadString
  620.     ELSE
  621.       SpecialCharacter
  622.     END
  623.     
  624.     9.6. While statements
  625.         While statements specify repetition.  If the Boolean expression
  626.     (guard)  yields  TRUE,  the  statement  sequence  is executed.  The
  627.     expression evaluation and the statement execution are  repeated  as
  628.     long  as  the  Boolean  expression yields TRUE.
  629.     
  630.     $ WhileStatement = WHILE expression DO StatementSequence END.
  631.     
  632.         Examples:
  633.     WHILE j > 0 DO
  634.       j := j DIV 2;
  635.       i := i+1
  636.     END
  637.     WHILE (t # NIL) & (t.key # i) DO
  638.       t := t.left
  639.     END
  640.     
  641.     9.7. Repeat Statements
  642.         A repeat  statement  specifies  the  repeated  execution  of  a
  643.     statement sequence until a condition is  satisfied.  The  statement
  644.     sequence is executed at least once.
  645.     
  646.     $ RepeatStatement  =  REPEAT StatementSequence UNTIL expression.
  647.     
  648.     9.8. Loop statements
  649.         A loop  statement  specifies  the  repeated  execution   of   a
  650.     statement  sequence.  It is terminated by the execution of any exit
  651.     statement within that sequence (see 9.9).
  652.     
  653.     $ LoopStatement  =  LOOP StatementSequence END.
  654.     
  655.     Example:
  656.     LOOP
  657.       IF t1 = NIL THEN
  658.         EXIT
  659.       END;
  660.       IF k < t1.key THEN
  661.         t2 := t1.left;
  662.         p := TRUE
  663.       ELSIF  k > t1.key THEN
  664.         t2 := t1.right;
  665.         p := FALSE
  666.       ELSE
  667.         EXIT
  668.       END;
  669.       t1 := t2
  670.     END
  671.     
  672.         Although while  and  repeat statements can be expressed by loop
  673.     statements containing a single exit statement, the use of while and
  674.     repeat  statements  is recommended in the most frequently occurring
  675.     situations,  where  termination  depends  on  a  single   condition
  676.     determined  either  at  the  beginning  or  the end of the repeated
  677.     statement sequence.  The loop statement is useful to express  cases
  678.     with several termination conditions and points.
  679.     
  680.     9.9. Return and exit statements
  681.         A return statement consists  of  the  symbol  RETURN,  possibly
  682.     followed  by  an  expression.  It  indicates  the  termination of a
  683.     procedure,  and the expression specifies the result of  a  function
  684.     procedure.  Its type must be identical to the result type specified
  685.     in the procedure heading (see Ch.  10). Function procedures require
  686.     the  presence  of  a  return statement indicating the result value.
  687.     There may be several, although only one will be executed. In proper
  688.     procedures,  a  return  statement  is  implied  by  the  end of the
  689.     procedure body.  An explicit return statement therefore appears  as
  690.     an  additional  (probably  exceptional) termination point.
  691.         An exit statement consists of the  symbol  EXIT.  It  specifies
  692.     termination  of  the enclosing loop statement and continuation with
  693.     the statement following that loop statement.  Exit  statements  are
  694.     contextually,   although   not  syntactically  bound  to  the  loop
  695.     statement which contains them.
  696.     
  697.     9.10. With statements
  698.         If a  pointer  variable  or  a  variable  parameter with record
  699.     structure is of a type T0, it may be designated in the heading of a
  700.     with clause together with a type T that is an extension of T0. Then
  701.     the variable is guarded within the with statement as if it had been
  702.     declared  of  type T.  The with statement assumes a role similar to
  703.     the type guard,  extending  the  guard  over  an  entire  statement
  704.     sequence. It may be regarded as a regional type guard.
  705.     
  706.     $ WithStatement = WITH qualident ":" qualident DO
  707.     $                   StatementSequence
  708.     $                 END.
  709.     
  710.         Example:
  711.     WITH t: CenterNode DO
  712.       name := t.name;
  713.       L := t.subnode
  714.     END
  715.     
  716.                         10. Procedure declarations
  717.     
  718.         Procedure declarations  consist  of  a  procedure heading and a
  719.     procedure body. The heading specifies the procedure identifier, the
  720.     formal parameters,  and the result type (if any). The body contains
  721.     declarations and statements.  The procedure identifier is  repeated
  722.     at  the  end  of the procedure declaration.  There are two kinds of
  723.     procedures, namely proper procedures and function procedures.
  724.         The latter   are  activated  by  a  function  designator  as  a
  725.     constituent of an expression, and yield a result that is an operand
  726.     in  the expression.  Proper procedures are activated by a procedure
  727.     call. The function procedure is distinguished in the declaration by
  728.     indication  of the type of its result following the parameter list.
  729.     Its body must contain a RETURN statement which defines  the  result
  730.     of the function procedure.
  731.         All constants, variables, types, and procedures declared within
  732.     a  procedure  body are local to the procedure.  The values of local
  733.     variables  are  undefined  upon  entry  to  the  procedure.   Since
  734.     procedures   may  be  declared  as  local  objects  too,  procedure
  735.     declarations may be nested.  In addition to its  formal  parameters
  736.     and   locally   declared  objects,  the  objects  declared  in  the
  737.     environment of the procedure are  also  visible  in  the  procedure
  738.     (with  the exception of those objects that have the same name as an
  739.     object declared locally).  The use of the procedure identifier in a
  740.     call  within  its  declaration  implies recursive activation of the
  741.     procedure.
  742.     
  743.     $ ProcedureDeclaration = ProcedureHeading ";" ProcedureBody ident.
  744.     $ ProcedureHeading = PROCEDURE ["*"] identdef [FormalParameters].
  745.     $ ProcedureBody = DeclarationSequence [BEGIN StatementSequence] END.
  746.     $ ForwardDeclaration = PROCEDURE "^" identdef [FormalParameters].
  747.     $ DeclarationSequence = {CONST {ConstantDeclaration ";"} |
  748.     $     TYPE {TypeDeclaration ";"} | VAR {VariableDeclaration ";"}}
  749.     $     {ProcedureDeclaration ";" | ForwardDeclaration ";"}.
  750.     
  751.         A forward declaration serves to allow forward references  to  a
  752.     procedure  that  appears  later  in  the  text in full.  The actual
  753.     declaration - which specifies the body -  must  indicate  the  same
  754.     parameters and result type (if any) as the forward declaration, and
  755.     it must be within the same scope.  An asterisk following the symbol
  756.     PROCEDURE  is  a  hint  to  the  compiler  and  specifies  that the
  757.     procedure is to be usable as parameter and assignable to  variables
  758.     of a compatible procedure type.
  759.     
  760.     10.1. Formal parameters
  761.         Formal parameters   are   identifiers   which   denote   actual
  762.     parameters  specified  in  the  procedure call.  The correspondence
  763.     between formal  and  actual  parameters  is  established  when  the
  764.     procedure  is  called.  There  are two kinds of parameters,  namely
  765.     value and variable parameters.
  766.         The kind  is  indicated  in  the  formal parameter list.  Value
  767.     parameters stand for local variables to which  the  result  of  the
  768.     evaluation  of  the  corresponding  actual parameter is assigned as
  769.     initial value.  Variable parameters correspond to actual parameters
  770.     that  are variables,  and they stand for these variables.  Variable
  771.     parameters are indicated by the symbol VAR, value parameters by the
  772.     absence of the symbol VAR.  A function procedure without parameters
  773.     must have an empty parameter list.  It must be called by a function
  774.     designator  whose  actual  parameter  list  is  empty  too.  Formal
  775.     parameters are local to the procedure,  i.e.  their  scope  is  the
  776.     program text which constitutes the procedure declaration.
  777.     
  778.     $ FormalParameters = "(" [FPSection {";" FPSection}] ")"
  779.     $                     [":" qualident].
  780.     $ FPSection  =  [VAR] ident {"," ident} ":" FormalType.
  781.     $ FormalType  =  {ARRAY OF} (qualident| ProcedureType).
  782.     
  783.         The type of each formal parameter is specified in the parameter
  784.     list.  For  variable  parameters,  it  must  be  identical  to  the
  785.     corresponding  actual  parameter's  type,  except  in the case of a
  786.     record,  where it must be a base type of the  corresponding  actual
  787.     parameter's  type.  For  value  parameters,  the rule of assignment
  788.     holds (see 9.1).  If the formal parameter's type  is  specified  as
  789.     ARRAY OF T the parameter is said to be an open array parameter, and
  790.     the corresponding actual parameter may be any  array  with  element
  791.     type T.  If a formal parameter specifies a procedure type, then the
  792.     corresponding actual parameter must be either a procedure  declared
  793.     at level 0 or a variable (or parameter) of that procedure type.  It
  794.     cannot be a predefined procedure.  The result type of  a  procedure
  795.     can be neither a record nor an array.
  796.         Examples of procedure declarations:
  797.     
  798.     PROCEDURE ReadInt(VAR x: INTEGER);
  799.       VAR i : INTEGER;
  800.       ch: CHAR;
  801.       BEGIN
  802.         i := 0;
  803.         Read(ch);
  804.         WHILE ("0"<= ch) & (ch <= "9") DO
  805.           i := 10*i + (ORD(ch)-ORD("0"));
  806.           Read(ch)
  807.         END ;
  808.         x := i
  809.       END ReadInt
  810.     
  811.     PROCEDURE WriteInt(x:INTEGER);
  812.      (* 0 <= x < 10^5 *)
  813.       VAR
  814.         i: INTEGER;
  815.         buf: ARRAY 5 OF INTEGER;
  816.       BEGIN
  817.         i := 0;
  818.         REPEAT
  819.           buf[i] := x MOD 10;
  820.           x := x DIV 10;
  821.           INC(i)
  822.         UNTIL x = 0;
  823.         REPEAT
  824.           DEC(i);
  825.           Write(CHR(buf[i]+ ORD("0")))
  826.         UNTIL i = 0
  827.       END WriteInt
  828.     
  829.     PROCEDURE log2(x: INTEGER): INTEGER;
  830.       VAR
  831.         y: INTEGER;
  832.        (*assume x>0*)
  833.       BEGIN
  834.         y := 0;
  835.         WHILE x > 1 DO
  836.           x := x DIV 2;
  837.           INC(y)
  838.         END;
  839.         RETURN y
  840.       END log2
  841.     
  842.     10.2. Predefined procedures
  843.         The following  table lists the predefined procedures.  Some are
  844.     generic procedures, i.e. they apply to several types of operands. v
  845.     stands for a variable, x and n for expressions, and T for a type.
  846.     
  847.     Function procedures:
  848.     Name     Argument type    Result type    Function
  849.     ABS(x)   numeric type     type of x      absolute value
  850.     ODD(x)   integer type     BOOLEAN        x MOD 2 = 1
  851.     CAP(x)    CHAR             CHAR           corresponding capital letter
  852.     ASH(x,n)  x, n: integer   LONGINT        x * 2^n,  arithmetic shift
  853.               type
  854.     LEN(v, n) v: array        LONGINT        the length of v in dimension n
  855.               n: integer type                LEN(v) is equivalent
  856.                                              with  LEN(v, 0)
  857.     MAX(T)    T = basic type  T              maximum value of type T
  858.               T = SET         INTEGER        maximum element of sets
  859.     MIN(T)    T = basic type  T              minimum value of type T
  860.               T = SET         INTEGER        0
  861.     SIZE(T)   T = any type    integer type   no. of bytes required by T
  862.     
  863.     Type conversion procedures:
  864.     Name      Argument type   Result type    Function
  865.     ORD(x)    CHAR            INTEGER        ordinal number of x
  866.     CHR(x)    integer type    CHAR           character with 
  867.                                              ordinal number x
  868.     SHORT(x)  LONGINT         INTEGER        identity 
  869.               INTEGER         SHORTINT
  870.               LONGREAL        REAL             (truncation possible)  
  871.     LONG(x)   SHORTINT        INTEGER        identity
  872.               INTEGER         LONGINT
  873.               REAL            LONGREAL
  874.     ENTIER(x) real type       LONGINT        largest integer
  875.                                              not greater than x
  876.                                              Note that 
  877.                                              ENTIER(i/j) = i DIV j
  878.      
  879.     Proper procedures:
  880.     Name       Argument types                  Function
  881.     INC(v)     integer type                    v := v+1
  882.     INC(v, x)  integer type                    v := v+x
  883.     DEC(v)     integer type                    v := v-1
  884.     DEC(v, x)  integer type                    v := v-x
  885.     INCL(v, x) v: SET; x: integer type         v := v + {x}
  886.     EXCL(v, x) v: SET; x: integer type         v := v - {x}
  887.     COPY(x, v) x: character array, string      v := x
  888.                v: character array
  889.     NEW(v)     pointer type                    allocate v^
  890.     HALT(x)    integer constant                terminate 
  891.                                                program execution
  892.     
  893.         The second parameter of INC and DEC may be  omitted,  in  which
  894.     case  its  default value is 1.  In HALT(x),  x is a parameter whose
  895.     interpretation is left to the underlying system implementation.
  896.     
  897.                                 11. Modules
  898.     
  899.         A module is a collection of declarations of  constants,  types,
  900.     variables,  and  procedures,  and  a sequence of statements for the
  901.     purpose of assigning initial values  to  the  variables.  A  module
  902.     typically constitutes a text that is compilable as a unit.
  903.     
  904.     $ module =  MODULE ident ";"  [ImportList] DeclarationSequence
  905.     $     [BEGIN StatementSequence] END ident "." .
  906.     $ ImportList  =  IMPORT import {"," import} ";" .
  907.     $ import  =  ident [":=" ident].
  908.     
  909.         The import list specifies the modules of which the module is  a
  910.     client. If an identifier x is exported from a module M, and if M is
  911.     listed in a module's import list,  then x is referred to as M.x. If
  912.     the form "M := M1" is used in the import list, that object declared
  913.     within M1 is referenced as M.x . Identifiers that are to be visible
  914.     in  client  modules,  i.e.  outside  the declaring module,  must be
  915.     marked by an  export  mark  in  their  declaration.  The  statement
  916.     sequence  following the symbol BEGIN is executed when the module is
  917.     added to a system (loaded).  Individual (parameterless)  procedures
  918.     can  thereafter be activated from the system,  and these procedures
  919.     serve as commands.
  920.         Example:
  921.     
  922.     MODULE Out;
  923.      (*exported procedures:  Write, WriteInt, WriteLn*)
  924.       IMPORT Texts, Oberon;
  925.       VAR
  926.         W: Texts.Writer;
  927.     
  928.       PROCEDURE Write*(ch: CHAR);
  929.         BEGIN
  930.           Texts.Write(W, ch)
  931.         END Write;
  932.       PROCEDURE WriteInt*(x, n: LONGINT);
  933.         VAR
  934.           i: INTEGER;
  935.           a: ARRAY 16 OF CHAR;
  936.         BEGIN
  937.           i := 0;
  938.           IF x < 0 THEN
  939.             Texts.Write(W, "-");
  940.             x := -x
  941.           END;
  942.           REPEAT
  943.             a[i] := CHR(x MOD 10 + ORD("0"));
  944.             x := x DIV 10;
  945.             INC(i)
  946.           UNTIL x = 0;
  947.           REPEAT
  948.             Texts.Write(W, " ");
  949.             DEC(n)
  950.           UNTIL n <= i;
  951.           REPEAT
  952.             DEC(i);
  953.             Texts.Write(W, a[i])
  954.           UNTIL i = 0
  955.         END WriteInt;
  956.     
  957.       PROCEDURE WriteLn*;
  958.         BEGIN
  959.           Texts.WriteLn(W);
  960.           Texts.Append(Oberon.Log, W.buf)
  961.         END WriteLn;
  962.     
  963.       BEGIN
  964.         Texts.OpenWriter(W)
  965.       END Out.
  966.     
  967.                            12. The Module SYSTEM
  968.     
  969.         The module SYSTEM contains definitions that  are  necessary  to
  970.     program   low-level  operations  referring  directly  to  resources
  971.     particular to a given computer and/or implementation. These include
  972.     for example facilities for accessing devices that are controlled by
  973.     the computer,  and facilities to break the data type  compatibility
  974.     rules   otherwise   imposed  by  the  language  definition.  It  is
  975.     recommended to restrict their use to  specific  low-level  modules.
  976.         Such modules are inherently non-portable, but easily recognized
  977.     due to the identifier SYSTEM appearing in their import  lists.  The
  978.     subsequent  definitions  are  applicable  to most modern computers;
  979.     however,  individual implementations may  include  in  this  module
  980.     definitions   that  are  particular  to  the  specific,  underlying
  981.     computer.  Module  SYSTEM  exports   the   data   type   BYTE.   No
  982.     representation   of   values   is   specified.   Instead,   certain
  983.     compatibility rules with other types are given:
  984.         1. The type BYTE is compatible with CHAR and SHORTINT.
  985.         2. If a formal variable parameter is of  type  ARRAY  OF  BYTE,  
  986.     then  the corresponding actual parameter may be of any type.
  987.         The procedures contained in module SYSTEM  are  listed  in  the
  988.     following  tables.  They correspond to single instructions compiled
  989.     as in-line code.  For  details,  the  reader  is  referred  to  the
  990.     processor  manual.  v stands for a variable,  x,  y,  a,  and n for
  991.     expressions, and T for a type.
  992.     
  993.     Function procedures:
  994.     Name      Argument types   Result type       Function
  995.     ADR(v)    any              LONGINT           address of variable v
  996.     BIT(a, n) a: LONGINT       BOOLEAN           bit n of Mem[a]
  997.                                                  n: integer type
  998.     CC(n)     integer constant BOOLEAN           Condition  n
  999.                                                  (0 <= n < 16)
  1000.     LSH(x, n) x: integer type  type of x         logical shift
  1001.               or SET
  1002.               n: integer type
  1003.     ROT(x, n) x: integer type  type of x         rotation
  1004.               or SET
  1005.               n: integer type
  1006.     VAL(T, x) T, x: any type   T                 x interpreted as
  1007.                                                  of type T
  1008.     
  1009.     Proper procedures:
  1010.     Name          Argument types                 Function
  1011.     GET(a, v)     a: LONGINT; v: any basic type  v := Mem[a]
  1012.     PUT(a, x)     a: LONGINT; x: any basic type  Mem[a] := x
  1013.     MOVE(s, d, n) s, d: LONGINT; n: integer type Mem[d] ... Mem[d+n-1] :=
  1014.                                                  Mem[s] ... Mem[s+n-1]
  1015.     NEW(v, n)     v: any pointer type            allocate storage block
  1016.                                                  of n bytes
  1017.                   n: integer type                assign  its  address to v
  1018.     
  1019.     File: OberonReport.Doc / NW 1.10.90
  1020.